Click here to Skip to main content
11,652,380 members (77,484 online)
Click here to Skip to main content

Tagged as

Servomotor Control with PWM and VHDL

, 20 Dec 2012 LGPL3 30.2K 575 3
Generate a PWM signal for servomotor control with VHDL.

Simulation of the PWM for servomotor

Introduction

In this brief article we generate a pulse width modulation signal for a servomotor control with VHDL.

Background

First of all, a servomotor is nothing more than a direct current motor with an electronic circuit attached in order to achieve better control. For such control, we need to generate a waveform like the one shown below:

PWM signal for servomotor control

The time, or frequency, of the pulse determines the position of the servo. Each servo has its own range of frequencies, given by the manufacturer in the data sheet. The figure shows values within 1 and 2 ms.

Design of the control

The control signal for the servomotor is made out of two frequencies:

  • Refresh frequency, of 20ms.
  • Pulse width range, provided by the manufacturer. For this article, we are going to assume such frequency goes from 0.5ms to 2.5ms.

First of all, we need to find our range of operation, given by:

Range of operation

Now we need to know the resolution for the servo, which is the quantity of position it can take. Therefore, the minimum needed frequency is equivalent to:

Minimum needed frequency

If our servomotor can take up to 128 position, we yield:

Minimum needed frequency, substitution

We now need a frequency divider of 64kHz. Although we can do one following the process described at frequency divider with VHDL, here we have the full code of the divider from 50MHz to 64kHz:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
 
entity clk64kHz is
    Port (
        clk    : in  STD_LOGIC;
        reset  : in  STD_LOGIC;
        clk_out: out STD_LOGIC
    );
end clk64kHz;
 
architecture Behavioral of clk64kHz is
    signal temporal: STD_LOGIC;
    signal counter : integer range 0 to 780 := 0;
begin
    freq_divider: process (reset, clk) begin
        if (reset = '1') then
            temporal <= '0';
            counter  <= 0;
        elsif rising_edge(clk) then
            if (counter = 780) then
                temporal <= NOT(temporal);
                counter  <= 0;
            else
                counter <= counter + 1;
            end if;
        end if;
    end process;
 
    clk_out <= temporal;
end Behavioral;

Finally, we know that with a 64kHz clock we will have 1ms each 64 iterations. So, in order to have a frequency of 20ms, we only need to multiply 20 * 64, which will be implemented with a counter from 0 to 1279.

Implementation in VHDL

For the VHDL implementation we have three inputs: 64 kHz clock, reset, and a vector that an take the values from 0 to 127. The only output is the servomotor control signal. The code is shown below.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity servo_pwm is
    PORT (
        clk   : IN  STD_LOGIC;
        reset : IN  STD_LOGIC;
        pos   : IN  STD_LOGIC_VECTOR(6 downto 0);
        servo : OUT STD_LOGIC
    );
end servo_pwm;

architecture Behavioral of servo_pwm is
    -- Counter, from 0 to 1279.
    signal cnt : unsigned(10 downto 0);
    -- Temporal signal used to generate the PWM pulse.
    signal pwmi: unsigned(7 downto 0);
begin
    -- Minimum value should be 0.5ms.
    pwmi <= unsigned('0' & pos) + 32;
    -- Counter process, from 0 to 1279.
    counter: process (reset, clk) begin
        if (reset = '1') then
            cnt <= (others => '0');
        elsif rising_edge(clk) then
            if (cnt = 1279) then
                cnt <= (others => '0');
            else
                cnt <= cnt + 1;
            end if;
        end if;
    end process;
    -- Output signal for the servomotor.
    servo <= '1' when (cnt < pwmi) else '0';
end Behavioral;

The signal cnt is used to implement the counter from 0 to 1279, which is described from lines 22 to 33.

The input signal pos is a vector that can take any value from 0 to 127, which yield the range of 0ms to 2ms. Since we need a signal that goes from 0.5ms to 2.5ms, we need to add an offset of 32 equivalent to the 0.5ms.

The listing 3, shown below, describes the PORT MAP needed to glue together the frequency divider and the servomotor control component.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity servo_pwm_clk64kHz is
    PORT(
        clk  : IN  STD_LOGIC;
        reset: IN  STD_LOGIC;
        pos  : IN  STD_LOGIC_VECTOR(6 downto 0);
        servo: OUT STD_LOGIC
    );
end servo_pwm_clk64kHz;

architecture Behavioral of servo_pwm_clk64kHz is
    COMPONENT clk64kHz
        PORT(
            clk    : in  STD_LOGIC;
            reset  : in  STD_LOGIC;
            clk_out: out STD_LOGIC
        );
    END COMPONENT;
    
    COMPONENT servo_pwm
        PORT (
            clk   : IN  STD_LOGIC;
            reset : IN  STD_LOGIC;
            pos   : IN  STD_LOGIC_VECTOR(6 downto 0);
            servo : OUT STD_LOGIC
        );
    END COMPONENT;
    
    signal clk_out : STD_LOGIC := '0';
begin
    clk64kHz_map: clk64kHz PORT MAP(
        clk, reset, clk_out
    );
    
    servo_pwm_map: servo_pwm PORT MAP(
        clk_out, reset, pos, servo
    );
end Behavioral;

Test bench and simulation

For the test bench, we use five different values for pos, described from lines 43 to 59.

LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
 
ENTITY servo_pwm_clk64kHz_tb IS
END servo_pwm_clk64kHz_tb;
 
ARCHITECTURE behavior OF servo_pwm_clk64kHz_tb IS
    -- Unit under test.
    COMPONENT servo_pwm_clk64kHz
        PORT(
            clk   : IN  std_logic;
            reset : IN  std_logic;
            pos   : IN  std_logic_vector(6 downto 0);
            servo : OUT std_logic
        );
    END COMPONENT;

    -- Inputs.
    signal clk  : std_logic := '0';
    signal reset: std_logic := '0';
    signal pos  : std_logic_vector(6 downto 0) := (others => '0');
    -- Outputs.
    signal servo : std_logic;
    -- Clock definition.
    constant clk_period : time := 10 ns;
BEGIN
    -- Instance of the unit under test.
    uut: servo_pwm_clk64kHz PORT MAP (
        clk => clk,
        reset => reset,
        pos => pos,
        servo => servo
    );

   -- Definition of the clock process.
   clk_process :process begin
        clk <= '0';
        wait for clk_period/2;
        clk <= '1';
        wait for clk_period/2;
   end process;
 
    -- Stimuli process.
    stimuli: process begin
        reset <= '1';
        wait for 50 ns;
        reset <= '0';
        wait for 50 ns;
        pos <= "0000000";
        wait for 20 ms;
        pos <= "0101000";
        wait for 20 ms;
        pos <= "1010000";
        wait for 20 ms;
        pos <= "1111000";
        wait for 20 ms;
        pos <= "1111111";
        wait;
    end process;
END;

The result of the simulation is shown below. The following frequencies were obtained:

  • A refresh frequency of 19.9936 ms.
  • A minimum frequency of 0.4920 ms.
  • A maximum frequency of 2.4835 ms.

Simulation of the PWM for servomotor

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Carlos A. Ramos
Engineer Estado Finito
Mexico Mexico
Mechatronics Engineer, with a biased interest in digital systems design and development. Currently studying Master's degree in Electrical Engineering and running a blog about digital design at Estado Finito.

You may also be interested in...

Comments and Discussions

 
You must Sign In to use this message board.
    Spacing  Noise  Layout  Per page   
Questionhow to interface servomotor with spartan 6 Pin
Member 114326251-Apr-15 19:36
memberMember 114326251-Apr-15 19:36 
GeneralMy vote of 3 Pin
Member 106529092-May-14 0:03
memberMember 106529092-May-14 0:03 
GeneralGood article Pin
HoshiKata25-Nov-13 15:37
memberHoshiKata25-Nov-13 15:37 
Questioncalculation for hbridge chip Pin
Member 1022559622-Aug-13 5:00
professionalMember 1022559622-Aug-13 5:00 
GeneralThanx Pin
urman ratneshwar27-Mar-13 0:38
memberurman ratneshwar27-Mar-13 0:38 
Generalservomotor and inputs Pin
Wolfagang9417-Mar-13 13:24
memberWolfagang9417-Mar-13 13:24 
Questionrefresh freq. Pin
urman ratneshwar10-Mar-13 23:10
memberurman ratneshwar10-Mar-13 23:10 
QuestionMessage Removed Pin
_beauw_20-Dec-12 11:02
member_beauw_20-Dec-12 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.4 | Last Updated 20 Dec 2012
Article Copyright 2012 by Carlos A. Ramos
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid

Original text